package com.bc.web.controller;

import java.text.DecimalFormat;
import java.util.TreeMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bc.web.config.SystemConfig;
import com.bc.web.model.TradeOrder;
import com.fs.common.extend.FSController;
import com.fs.common.util.FSHttp;
import com.fs.common.util.FSIDGenerate;
import com.fs.common.util.FSMD5;
import com.fs.common.util.FSMoney;

public class CustomerController extends FSController {

	private final String apiUrl = SystemConfig.config.get("customer_apiUrl");
	private final String orgCode = SystemConfig.config.get("customer_orgCode");
	private final String merchCode = SystemConfig.config.get("customer_merchCode");
	private final String safeKey = SystemConfig.config.get("customer_safeKey");
	private final String notifyUrl = SystemConfig.config.get("customer_notifyUrl");
	private final String returnUrl = SystemConfig.config.get("customer_returnUrl");
	
	
	public void index() {
		renderHtml("hello, word");
	}
	
	
	public void backNotify() {
		
		renderText("SUCCESS");
	}
	
	public void result() {
		String orderNo = getPara("outTradeNo");
		TradeOrder order = TradeOrder.dao.selectByOrderNo(orderNo);
		if(order == null) {
			setAttr("message", "订单不存在");
		}else {
			String amt = new DecimalFormat("0.00").format(order.getAmount()/100.00);
			setAttr("merchOrderNo", order.getMerchOrderNo());
			setAttr("amount", amt);
			setAttr("code", order.getRespCode());
			setAttr("message", order.getRespMsg());
			setAttr("createTime", order.getCreateTime());
		}
		setAttr("page", "result");
		render("casherdesk.html");
	}
	
	public void casherdesk() {
		setAttr("page", "payment");
		setAttr("merchOrderNo", FSIDGenerate.nextOrderNo());
		render("casherdesk.html");
	}
	
	public void order() {
		Integer amount = getParaToInt("amount") * 100;
		String merchOrderNo = getPara("merchOrderNo");
		setAttr("page", "result");
		setAttr("amount", FSMoney.changeF2Y(Long.valueOf(amount)));
		setAttr("merchOrderNo", merchOrderNo);
		String bank = getPara("bank");
		
		TreeMap<String, String> reqMap = new TreeMap<>();
		reqMap.put("version","V1.0");
		reqMap.put("orgCode", orgCode);
		reqMap.put("merchCode", merchCode);
		reqMap.put("payType","union_pay");
		reqMap.put("merchOrderNo",merchOrderNo);
		reqMap.put("amount",Integer.toString(amount));
		reqMap.put("cardType","0");
		reqMap.put("goodsName","客户货品");
		reqMap.put("bankType",bank);
		reqMap.put("orderPeriod","2");
		reqMap.put("notifyUrl",notifyUrl);
		reqMap.put("callbackUrl",returnUrl);
		reqMap.put("remark","外部订单");
		reqMap.put("sign", FSMD5.sortSign(reqMap, safeKey));
		
		String reqJson = JSON.toJSONString(reqMap);
		System.out.println("请求支付接口：" + reqJson);
		try {
			String rspJson = FSHttp.postJson(apiUrl, reqJson);
			System.out.println("支付接口响应报文：" + rspJson);
			JSONObject rspMap = JSON.parseObject(rspJson);
			
			String html = rspMap.getString("html");
			if(html != null) {
				renderHtml(html);
			}else {
				setAttr("message", rspMap.getString("respMsg"));
				render("casherdesk.html");
			}
		} catch (Exception e) {
			e.printStackTrace();
			setAttr("message", "出错了(" + e.getMessage() + ")");
			render("casherdesk.html");
		}
	}
}
